package cn.lyvust.first.api;


import cn.lyvust.first.common.annotation.SysLogs;
import cn.lyvust.first.common.bean.ResponseCode;
import cn.lyvust.first.common.bean.ResponseResult;
import cn.lyvust.first.common.exception.RequestException;
import cn.lyvust.first.common.util.BeansUtil;
import cn.lyvust.first.manage.dto.param.evaluation.ProjectListParam;
import cn.lyvust.first.manage.entity.EvaluationProject;
import cn.lyvust.first.manage.service.EvaluationProjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author ggzheng
 * @since 2021-01-19
 */
@Api(tags = {"所属项目"})
@RestController
@RequestMapping("/evaluation/project")
public class EvaluationProjectController {
    @Autowired
    private EvaluationProjectService evaluationProjectService;


    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询所属项目")
    public ResponseResult list(@RequestBody ProjectListParam param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        if (!StringUtils.isEmpty(param.getOperator())) {
            wrapper.eq("operator", param.getOperator());
        }
        if (!StringUtils.isEmpty(param.getCreateAt())) {
            wrapper.eq("create_at", param.getCreateAt());
        }
        if (!StringUtils.isEmpty(param.getCreateBy())) {
            wrapper.eq("create_by", param.getCreateBy());
        }
        if (!StringUtils.isEmpty(param.getProjectName())) {
            wrapper.eq("project_name", param.getProjectName());
        }
        wrapper.orderByDesc("update_time");
        return ResponseResult.e(ResponseCode.OK, evaluationProjectService.list(wrapper));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个所属项目")
    @SysLogs("新增一个所属项目")
    public ResponseResult add(@RequestBody ProjectListParam param){
        if (StringUtils.isEmpty(param.getProjectName())) {
            throw RequestException.fail("添加失败,所属项目名称不能为空!");
        }
        if (StringUtils.isEmpty(param.getCreateBy())) {
            throw RequestException.fail("添加失败,所属项目添加操作人姓名不能为空!");
        }
        EvaluationProject entity = new EvaluationProject();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK, evaluationProjectService.saveOrUpdate(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个所属项目")
    @SysLogs("修改一个所属项目")
    public ResponseResult update(@RequestBody ProjectListParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,所属项目主键ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getCreateBy())) {
            throw RequestException.fail("修改失败,修改操作人姓名不能为空!");
        }

        EvaluationProject entity = evaluationProjectService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, evaluationProjectService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个所属项目")
    @SysLogs("删除一个所属项目")
    public ResponseResult delete(@RequestBody ProjectListParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,所属项目主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, evaluationProjectService.removeById(param.getId()));
    }
}
